import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import "./index.less";
import { BasicColumn, BasicTable, FormSchema, useTable } from "@/components/Table";
import { patServicesBatchcreate, servicesPage } from "@/api/SecondmentDictionary";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { message } from "antd";
const index = (props: { register: Fn }) => {
	const { t } = useTranslation();
	const en_vi = "MedicalExamination.personalPhysicalExamination";
	const [services, setServices] = useState([]);
	const [record, setRecord] = useState([]);
	const schemas: FormSchema[] = [
		{
			label: t(`${en_vi}.serviceName`),
			field: "svcName",
			component: "Input"
		},
		{
			label: t(`${en_vi}.serviceRoomNumber`),
			field: "svcRoomNumb",
			component: "Input"
		}
	];
	const columns: BasicColumn[] = [
		{
			title: t(`${en_vi}.serviceName`),
			dataIndex: "svcName"
		},
		{
			title: t(`${en_vi}.serviceCode`),
			dataIndex: "svcCode"
		},
		{
			title: t(`${en_vi}.serviceRoomNumber`),
			dataIndex: "svcRoomNumb"
		},
		{
			title: t(`${en_vi}.servicePrice`),
			dataIndex: "svcPrice"
		},
		{
			title: t(`${en_vi}.quantity`),
			dataIndex: "quantity",
			edit: true,
			width: 200
		},
		{
			title: t(`${en_vi}.action`),
			dataIndex: "svcAction"
		},
		{
			title: t(`${en_vi}.Description`),
			dataIndex: "description"
		}
	];
	const [registerDrawer, { closeDrawer }] = useDrawerInner(props.register, async data => {
		// reload = data.reload;
		console.log(data);
		setRecord(data.record);
	});

	const [register] = useTable({
		api: servicesPage,
		columns,
		useSearchForm: true,
		formConfig: {
			labelWidth: 120,
			schemas,
			showActionButtonGroup: true,
			showAdvancedButton: false,
			baseColProps: { span: 12 },
			actionColOptions: { span: 24 }
		},
		rowSelection: {
			type: "checkbox",
			onChange: (_: any, record: any) => {
				console.log(record);
				setServices(record);
			}
		},
		bordered: false
	});
	const handleSubmit = async () => {
		const res = await patServicesBatchcreate({
			...record,
			svcType: 1,
			services
		});
		console.log(res);
		closeDrawer();
		if (!res) return message.error(t("kevin_ui.addFailed"));
		message.success(t("kevin_ui.addedSuccessfully"));
	};
	return (
		<BasicDrawer showFooter={true} size="large" {...props} register={registerDrawer} ok={handleSubmit}>
			<div className="warehouses">
				<BasicTable register={register}></BasicTable>
			</div>
		</BasicDrawer>
	);
};
export default index;
